package com.lcq.springbootcreate.project.controller;

import com.lcq.springbootcreate.project.ExceptionAndEnum.MallException;
import com.lcq.springbootcreate.project.ExceptionAndEnum.MallExceptionEnum;
import com.lcq.springbootcreate.project.common.ApiRestResponse;
import com.lcq.springbootcreate.project.common.Constant;
import com.lcq.springbootcreate.project.entity.User;
import com.lcq.springbootcreate.project.service.UserService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping({"test"})
    public String getUserByIdFour() {
        return userService.getUserById(4).toString();
    }
    @PostMapping("/register")
    public ApiRestResponse register(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) throws MallException {
        if (StringUtils.isNullOrEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isNullOrEmpty(passWord)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASS_WORD);

        }
        if (passWord.length()<8){
            return ApiRestResponse.error(MallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        System.out.println(userService.register(userName,passWord));
        return ApiRestResponse.success();
    }
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, HttpSession session) throws MallException {
        if (StringUtils.isNullOrEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isNullOrEmpty(passWord)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASS_WORD);
        }
        User user = userService.login(userName,passWord);
        // 返回用户信息不反悔密码
        user.setPassword(null);
        // 登陆信息放到session对象
        session.setAttribute(Constant.MALL_USER,user);
        return ApiRestResponse.success(user);
    }
    @PostMapping("/user/update")
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam String signature) throws MallException {
        User currentUser = (User)session.getAttribute(Constant.MALL_USER);
        if (currentUser == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        System.out.println("当前的用户名字："+currentUser.getUsername());
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalized_signature(signature);
        System.out.println("准备修改数据新建的user:"+user.toString());
        userService.updateSignatureByUser(user);
        return ApiRestResponse.success();
    }
    @PostMapping("/user/loginout")
    public ApiRestResponse logout(HttpSession session){
        session.removeAttribute(Constant.MALL_USER);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/login")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, HttpSession session) throws MallException {
        if (StringUtils.isNullOrEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isNullOrEmpty(passWord)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASS_WORD);
        }
        User user = userService.login(userName,passWord);
        if (userService.checkAdminRole(user)) {
            // 是管理员
            // 返回用户信息不反悔密码
            user.setPassword(null);
            // 登陆信息放到session对象
            session.setAttribute(Constant.MALL_USER,user);
            return ApiRestResponse.success(user);
        }

        return ApiRestResponse.error(MallExceptionEnum.NOT_ADIMIN);
    }
}
